package cn.huiyunche.admin.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;

import cn.huiyunche.admin.utils.AdminUtils;
import cn.huiyunche.base.service.interfaces.CWaybillService;
import cn.huiyunche.base.service.model.SysAdminUser;
import cn.huiyunche.base.service.utils.UniquenessFlagUnits;
import cn.huiyunche.base.service.vo.CWaybillByIdVo;
import cn.huiyunche.base.service.vo.CWaybillDetailsVo;
import cn.huiyunche.base.service.vo.PageVo;
import cn.huiyunche.base.service.vo.Result;
import cn.huiyunche.base.service.vo.WaybillContactsVo;
import cn.huiyunche.base.service.vo.WaybillNewsVo;

/**
 * 运单Controller
 * 
 * @author Administrator
 *
 */
@RequestMapping("/waybill")
@Controller
public class CWaybillController {

	private static final Logger logger = LoggerFactory.getLogger(CWaybillController.class);

	@Autowired
	private CWaybillService cWaybillService = null;

	/**
	 * 查询参考价格－运费
	 * 
	 * @return
	 * @throws Exception
	 */
//	@RequestMapping(value = "/price", method = RequestMethod.POST)
//	@ResponseBody
//	public Result<String> selectPrice(CWaybillDetailsVo cWaybillDetailsVo) throws Exception {
//		Result<String> result = new Result<String>(true, null, "获取参考价格成功");
//		result.setData(this.getCWaybillService().selectPrice(cWaybillDetailsVo));
//		return result;
//	}
	
	@RequestMapping(value = "/veneercreate", method = RequestMethod.POST)
	@ResponseBody
	public Result<String> createVeneer(Long serviceOrderId, Long bidderId, Integer waybillType) throws Exception {
		logger.info("create veneer cwaybill params serviceOrderId : {}.", serviceOrderId);
		SysAdminUser sysAdminUser = AdminUtils.getSysAdminUser();
		return this.getCWaybillService().createVeneer(serviceOrderId, bidderId, waybillType, sysAdminUser);
	}

	/**
	 * 创建运单
	 * 
	 * @param cWaybillDetailsVo
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/create", method = RequestMethod.POST)
	@ResponseBody
	public Result<String> create(CWaybillDetailsVo cWaybillDetailsVo) throws Exception {
		logger.info("create cwaybill params cWaybillDetailsVo : {}.", JSON.toJSONString(cWaybillDetailsVo));
		SysAdminUser adminUser = AdminUtils.getSysAdminUser();
		return this.getCWaybillService().create(cWaybillDetailsVo, adminUser);
	}

	/**
	 * 根据服务订单ID 查询运单
	 * 
	 * @param serviceOrderId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/selectbysid", method = RequestMethod.GET)
	public Result<List<CWaybillByIdVo>> selectBySid(Long serviceOrderId) {
		Result<List<CWaybillByIdVo>> result = new Result<List<CWaybillByIdVo>>(true, null, "数据加载成功");
		logger.info("select waybill by sservice orderid params serviceOrderId : {}.", serviceOrderId);
		result.setData(this.getCWaybillService().selectBySid(serviceOrderId));
		return result;
	}

	/**
	 * 根据运单ID 查询运单详情
	 * 
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/selectbyid", method = RequestMethod.GET)
	public Result<CWaybillByIdVo> selectById(Long id) {
		Result<CWaybillByIdVo> result = new Result<CWaybillByIdVo>(true, null, "数据加载成功");
		logger.info("select CWaybillByIdVo by id params id : {}.", id);
		result.setData(this.getCWaybillService().selectById(id));
		return result;
	}

	@RequestMapping(value = "/loadwaybillcode", method = RequestMethod.GET)
	@ResponseBody
	public Result<String> loadwaybillcode() throws Exception {
		Result<String> result = new Result<String>(true, null, "获取编码成功");
		result.setData(UniquenessFlagUnits.generateUniquenessFlag("W"));
		return result;
	}

	/**
	 * 查询运单列表
	 * 
	 * @param page
	 * @param waybillVo
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public Result<Object> list(PageVo page, WaybillNewsVo waybillVo) throws Exception {
		Result<Object> result = new Result<Object>(true, null, "数据加载成功");
		result.setData(this.getCWaybillService().list(page, waybillVo));
		return result;
	}

	/**
	 * 查询交接单信息
	 * 
	 * @param vo
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "selectwaybillcontact", method = RequestMethod.GET)
	public Result<WaybillContactsVo> selectwaybillcontact(WaybillContactsVo vo) throws Exception {
		Result<WaybillContactsVo> result = new Result<WaybillContactsVo>(true, null, "查询交接单信息成功");
		result.setData(this.getCWaybillService().selectWaybillContact(vo));
		return result;
	}

	/**
	 * 提车审核通过
	 * 
	 * @param waybillId
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/liftaudit", method = RequestMethod.POST)
	public Result<Object> liftAudit(Long waybillId) throws Exception {
		Result<Object> result = new Result<Object>(true, null, "提车审核成功");
		result.setData(this.getCWaybillService().liftAudit(waybillId));
		return result;
	}
	
	/**
	 * 交车审核通过
	 * 
	 * @param waybillId
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/dealaudit", method = RequestMethod.POST)
	public Result<Object> dealAudit(Long waybillId) throws Exception {
		Result<Object> result = new Result<Object>(true, null, "交车审核成功");
		result.setData(this.getCWaybillService().dealAudit(waybillId));
		return result;
	}
	
	@ResponseBody
	@RequestMapping(value = "/proposedprice", method = RequestMethod.GET)
	public Result<Object> proposedPrice(String depaCode, String depaAddr, String destCode, String destAddr, String adate, int atime, String serviceOrderIds){
		logger.info("proposedPrice params depaCode: {}, depaAddr: {}, destCode: {}, destAddr: {}, adate: {}, atime: {}, serviceOrderIds: {}", depaCode, depaAddr, destCode, destAddr, adate, atime, serviceOrderIds);
		Result<Object> result = new Result<Object>(true, null, "查询成功");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		List<Long> serviceOrderId = JSONArray.parseArray(serviceOrderIds, Long.class);
		try {
			result.setData(this.getCWaybillService().proposedPrice(depaCode, depaAddr, destCode, destAddr, adate == ""? null: sdf.parse(adate), atime, serviceOrderId));
		} catch (ParseException e) {
			logger.error("convert date error: {}", e);
		}
		return result;
	}
	
	@ResponseBody
	@RequestMapping(value = "/updatepaystatus", method = RequestMethod.POST)
	public Result<String> updatePayStatus(Long waybillId){
		Result<String> result = new Result<>(true, "运单改为已支付状态完成");
		int count = this.getCWaybillService().updatePayStatus(waybillId);
		if(count != 1){
			result.setSuccess(false);
			result.setMessage("运单改为已支付状态系统异常");
		}
		return result;
	}

	private CWaybillService getCWaybillService() {
		return this.cWaybillService;
	}

}
